<?php
//optional:
//$_GET['time_start']=0 起始时间
//$_GET['time_end']=now 终止时间
//$_GET['type']={node|group}
//$_GET['title']
//$_GET['rate']={all|rate_array} 只能看自己的评级，没有值则表示查询所有
//$_GET::pager

	include 'include/function.php';
	$page_title=$site_title . ' - ' . _d('title_search');
	include 'include/header.php';
	include 'include/pager.php';
	
	if(!isset($_GET['type']))$search_type='node';
	else $search_type=$_GET['type'];
	if($search_type!='node'&&$search_type!='group')redirect_error('bad request');
	
	draw_search_box();
	$search_pager=new Pager;
	
function append_condition($where,$condition) {
	if(!empty($where)) return $where . 'and ' . $condition;
	else return 'where ' . $condition;
}

function is_updated($node,$user) {
	if(!isset($user))return '';
	$query='select (user_rate.visit_time<node.modify_time) as is_new, (user_rate.visit_time<node.child_update_time) as is_update '
		. "from node left join user_rate on node.id=user_rate.node_id and user_rate.user_id=$user "
		. "where node.id=$node limit 1 ";
	$ret=oj_query($query);
	$row=mysql_fetch_row($ret);
	if((!isset($row)) || is_null($row[0]) || $row[0])return '<span style="color:red">new!</span>';
	else if($row[1])return '<span style="color:red">update!</span>';
	else return '';
}

function check_param($key) {
	global $search_base_page;
	if(!empty($_GET[$key])) {
		$search_base_page=url_append_param($search_base_page,$key . '=' . urlencode($_GET[$key]));
		return true;
	} else return false;
}

	$search_base_page = 'search.php';
	check_param('type');
	if($search_type=='node') {
		$search_base = 'select id,title,child_count,modify_time,description="" from node ';
		if(check_param('rate')) {
			if(!isset($_user_id))redirect_error(_d('err_need_login'));
			$search_tmp=get_to_mysql($_GET['rate']);
			$search_base .= "inner join user_rate on node.id=user_rate.node_id and user_id=$_user_id ";
			if($search_tmp!='all')
				$search_base .= "and rate='$search_tmp' ";
		}
	} else {
		$search_base = 'select id,title,count,modify_time,description="",parent_count from `group` ';
	}
	
	$search_where = '';
	if(check_param('time_start')) {
		$search_tmp=get_to_mysql($_GET['time_start']);
		$search_where=append_condition($search_where, "modify_time>'$search_tmp' ");
	}
	if(check_param('time_end')) {
		$search_tmp=get_to_mysql($_GET['time_end']);
		$search_where=append_condition($search_where, "modify_time<'$search_tmp' ");
	}
	if(check_param('title')) {
		$search_tmp=get_to_mysql($_GET['title']);
		$search_where=append_condition($search_where, "title like '%$search_tmp%' ");
	}
	$search_order = 'order by modify_time desc ';
	
	$query=$search_base . $search_where . $search_order . $search_pager->mysql();
	$ret=oj_query($query);
	
	if($search_type=='node')
		draw_tablelist_head(array('#',array(_d('n_title'),'width=60%'),_d('n_edit')));
	else
		draw_tablelist_head(array('#',array(_d('g_title'),'width=60%'),_d('n_edit')));

	while($row=mysql_fetch_row($ret)) {
		$now_id=$row[0];
		$now_title=htmlspecialchars($row[1]);
		$now_child=$row[2];
		$now_time=$row[3];
		if($row[4])$now_empty='style="color:#d00;" title="empty"';
		else $now_empty='';
		
		echo '<tr>';
		echo '<td>' . ($search_pager->current_no()+1) . '</td>';
		if($search_type=='node') {
			$search_perm=permission_node($_user_id,$now_id);
			if(strstr($search_perm[0],'view')) {
				echo "<td><a href='shownode.php?id=$now_id' $now_empty>$now_title</a>($now_child)" . is_updated($now_id,$_user_id) . '</td>';
			} else {
				echo "<td><a href='shownode.php?id=$now_id' $now_empty>Node $now_id</a>($now_child)<span style='color:gray'>Access denied</span></td>";
			}
		} else {
			echo "<td><a href='group.php?action=show&id=$now_id' $now_empty>$now_title</a>($now_child/{$row[5]})</td>";
		}
		echo "<td align=center>$now_time</td>";
		echo '</tr>';
		$search_pager->count++;
	}
	
	echo '</table>';
	
	echo '<center>';
	echo $search_pager->html_top_page($search_base_page);
	echo $search_pager->html_prev_page($search_base_page);
	echo $search_pager->html_next_page($search_base_page);
	echo '</center>';
	
	include 'include/footer.php';
?>

